import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { ApiService, JsonResult } from './api.service';
import { PageVo, UserListVo, TeamVo, SuperUserListVo, AddVo } from 'src/app/shared/models';

@Injectable({
  providedIn: 'root'
})
export class UserService {

  constructor(private http: HttpClient,
    private apiService: ApiService
  ) { }

  /**
   * 用户列表
   * @param pageIndex 页面
   * @param pageSize 一页的数据
   * @param filter 过滤条件
   */
  getUserPage(pageIndex: number = 1, pageSize: number = 10, filter: string): Observable<PageVo<UserListVo>> {
    if (filter) {
      if (filter.length > 1 && filter.length < 50) {
        const params = new HttpParams()
          .append('pageIndex', `${pageIndex}`)
          .append('pageSize', `${pageSize}`)
          .append('filter', filter);
        return this.apiService.get('/user/all', { params })
          .pipe(
            map((jsonResult: JsonResult<PageVo<UserListVo>>) => jsonResult.data)
          );
      }
    } else {
      const params = new HttpParams()
        .append('pageIndex', `${pageIndex}`)
        .append('pageSize', `${pageSize}`);
      console.log('Janine===params==3==', params);
      return this.apiService.get('/user/all', { params })
        .pipe(
          map((jsonResult: JsonResult<PageVo<UserListVo>>) => jsonResult.data)
        );
    }
  }

  getSuperUsers(pageIndex: number, pageSize: number, filter: string): Observable<PageVo<SuperUserListVo>> {
    if (filter) {
      if (filter.length > 1 && filter.length < 50) {
        const params = new HttpParams()
          .append('pageIndex', `${pageIndex}`)
          .append('pageSize', `${pageSize}`)
          .append('filter', filter);
        console.log('Janine===params==2==', params);
        return this.apiService.get('/organization/list', { params })
          .pipe(
            map((jsonResult: JsonResult<PageVo<SuperUserListVo>>) => jsonResult.data)
          );
      }
    } else {
      const params = new HttpParams()
        .append('pageIndex', `${pageIndex}`)
        .append('pageSize', `${pageSize}`);
      console.log('Janine===params==3==', params);
      return this.apiService.get('/organization/list', { params })
        .pipe(
          map((jsonResult: JsonResult<PageVo<SuperUserListVo>>) => jsonResult.data)
        );
    }
  }


  /**
     * 企业管理员下的用户管理列表
     * @param pageIndex 页面
     * @param pageSize 一页的数据
     * @param filter 过滤条件
    */
  getOrgOfUserPage(pageIndex: number = 1, pageSize: number = 10, filter: string): Observable<PageVo<UserListVo>> {
    if (filter !== null && filter !== undefined && filter !== '') {
      if (filter.length > 1 && filter.length < 50) {
        const params = new HttpParams()
          .append('pageIndex', `${pageIndex}`)
          .append('pageSize', `${pageSize}`)
          .append('filter', filter);
        return this.apiService.get('/group/user/list', { params })
          .pipe(
            map((jsonResult: JsonResult<PageVo<UserListVo>>) => jsonResult.data)
          );
      }
    } else {
      const params = new HttpParams()
        .append('pageIndex', `${pageIndex}`)
        .append('pageSize', `${pageSize}`);
      console.log('Janine===params==3==', params);
      return this.apiService.get('/group/user/list', { params })
        .pipe(
          map((jsonResult: JsonResult<PageVo<UserListVo>>) => jsonResult.data)
        );
    }
  }


  getPassword(info: any) {
    console.log('=== getPassword.info ===>', info);
    return this.apiService.post('/user/password', info)
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  saveGroup(email: string, groupid: string, roleid: string) {
    console.log(`${email},${groupid},${roleid}`);
    return this.apiService.put('/saveGroup',
      {
        'email': email,
        'groupid': groupid,
        'roleid': roleid
      })
      .pipe(
        map((jsonResult: JsonResult<PageVo<UserListVo>>) => jsonResult.data)
      );
  }

  saveGroupUser(info: any) {
    return this.apiService.post('/group/add', info)
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  saveCommonUser(info: any) {
    return this.apiService.post('/group/user/add', info)
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  saveUser(info: any) {
    return this.apiService.post('/user/saveUser', info)
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  saveOrgUser(info: any) {
    return this.apiService.post('/organization/add', info)
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  /**
   * 企业用户
   * @param info 传过来的信息
   */
  updateGroupUser(info: any) {
    return this.apiService.post('/group/update', info)
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  /**
   * 企业用户的管理员
   * @param info 传过来的信息
   */
  updateAdminUser(info: any) {
    return this.apiService.post('/org/user/update', info)
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  /**
   * org 的小组用户
   * @param info  传过来的信息
   */
  updateOrgUser(info: any) {
    return this.apiService.post('/org/organization/update', info)
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  /**
   * org 的用户管理
   * @param info 传过来的信息
   */
  updateUser(info: any) {
    return this.apiService.post('/group/user/update', info)
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  /**
   * group(小组) 的用户管理
   * @param info 传过来的数据
   */
  updateCommonUser(info: any) {
    return this.apiService.post('/user/updateUser', info)
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  deleteOrgUser(info: any) {
    return this.apiService.post('/org/user/delete', info)
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  deleteGroupUser(groupUuid: string) {
    return this.apiService.get('/group/delete?groupUuid=' + groupUuid)
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  deleteCommonUser(userUuid: string) {
    return this.apiService.get('/group/user/delete?userUuid=' + userUuid)
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  deleteUser(userUuid: string) {
    return this.apiService.get('/user/delete?userUuid=' + userUuid)
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  /**
   * 判断超级管理员，创建企业用户时，企业名称是否重复
   */
  isOrgNameRepeat(orgName: string) {
    const params = new HttpParams()
      .append('orgName', `${orgName}`);
    return this.apiService.get('/check/orgname', { params })
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  /**
   * 判断新注册的邮箱是否重复
   */
  isEmailRepeat(email: string) {
    const params = new HttpParams()
      .append('email', `${email}`);
    return this.apiService.get('/check/email', { params })
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }

  /**
   * 判断小组用户，创建和编辑小组用户时，小组名称是否重复
   */
  isGroupNameRepeat(groupName: string, groupUuid?: string) {
    if (groupUuid) {
      const params = new HttpParams()
        .append('groupName', `${groupName}`)
        .append('groupUuid', `${groupUuid}`);
      return this.apiService.get('/group/check/groupname', { params })
        .pipe(
          map((jsonResult: JsonResult<AddVo>) => jsonResult)
        );
    } else {
      const params = new HttpParams()
        .append('groupName', `${groupName}`);
      return this.apiService.get('/group/check/groupname', { params })
        .pipe(
          map((jsonResult: JsonResult<AddVo>) => jsonResult)
        );
    }

  }


}
